package com.qingcloud.adminbackend.controller;

import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.sysstationmo.AddStationMo;
import com.qingcloud.adminbackend.entity.sysstationmo.StationMoDTO;
import com.qingcloud.adminbackend.service.ISysStationMoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Api(value = "监控对象", description = "监控对象")
@RestController
@RequestMapping("/station-mo")
public class SysStationMoController {

    @Resource
    private ISysStationMoService sysStationMoService;

    @ApiOperation(value = "获取所有楼宇的名称", notes = "获取所有楼宇的名称", response = StationMoDTO.class)
    @GetMapping(value = "/builds/{streetName}/{domainId}")
    public AjaxResult getAllBuildNames(@PathVariable("streetName") String streetName, @PathVariable("domainId") String domainId) {
        List<StationMoDTO> result = sysStationMoService.getAllBuildNames(streetName, domainId);
        return new AjaxResult(WamingConstant.SUCCESS, "", result);
    }

    @ApiOperation(value = "获取指定楼宇下的所有楼层", notes = "获取指定楼宇下的所有楼层", response = StationMoDTO.class)
    @ApiImplicitParam(name = "buildName", value = "楼宇名称", dataType = "string", paramType = "path")
    @GetMapping(value = "/floors/{streetName}/{buildName}/{domainId}")
    public AjaxResult getAllFloorNames(@PathVariable("streetName") String streetName,
                                       @PathVariable("buildName") String buildName,
                                       @PathVariable("domainId") String
                                               domainId) {
        List<StationMoDTO> result = sysStationMoService.getAllFloorNames(streetName, buildName, domainId);
        return new AjaxResult(WamingConstant.SUCCESS, "", result);
    }

    @ApiOperation(value = "获取指定楼层下的所有房间", notes = "获取指定楼层下的所有房间", response = StationMoDTO.class)
    @ApiImplicitParam(name = "floorName", value = "楼层名称", dataType = "string", paramType = "path")
    @GetMapping(value = "/rooms/{streetName}/{buildName}/{floorName}/{domainId}")
    public AjaxResult getAllRoomNames(@PathVariable("streetName") String streetName,
                                      @PathVariable("buildName") String buildName,
                                      @PathVariable("floorName") String floorName,
                                      @PathVariable("domainId") String domainId) {
        List<StationMoDTO> result = sysStationMoService.getAllRoomNames(streetName, buildName, floorName, domainId);
        return new AjaxResult(WamingConstant.SUCCESS, "", result);
    }

    @ApiOperation(value = "区域下的街道", notes = "区域下的街道", response = StationMoDTO.class)
    @ApiImplicitParam(name = "domainId", value = "区域ID", dataType = "string", paramType = "path")
    @GetMapping(value = "/street/{domainId}")
    public AjaxResult getStreetNames(@PathVariable("domainId") String domainId) {
        List<StationMoDTO> result = sysStationMoService.getStreetNames(domainId);
        return new AjaxResult(WamingConstant.SUCCESS, "", result);
    }

    @ApiOperation(value = "获取监测位置父级", notes = "获取监测位置父级")
    @GetMapping(value = "/getStationMoFather")
    public AjaxResult getStationMoFather() {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysStationMoService.getStationMoFather());
    }

    @ApiOperation(value = "获取监测点所属", notes = "获取监测点所属")
    @GetMapping(value = "/getStationMoBelong")
    public AjaxResult getStationMoBelong() {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysStationMoService.getStationMoBelong());
    }

    @ApiOperation(value = "获取监测点类型", notes = "获取监测点类型")
    @GetMapping(value = "/getStationMoType")
    public AjaxResult getStationMoType() {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysStationMoService.getStationMoType());
    }

    @ApiOperation(value = "添加监测位置", notes = "添加监测位置")
    @PostMapping(value = "/addStationMo")
    public AjaxResult addStationMo(@RequestBody AddStationMo addStationMo) throws IOException {
        //添加对象
        AddStationMo newMo = sysStationMoService.addStationMo(addStationMo);
        //保存图片
        sysStationMoService.saveMoImage(newMo);
        return new AjaxResult(WamingConstant.SUCCESS, "", "添加成功");
    }

    @ApiOperation(value = "删除监测位置", notes = "删除监测位置")
    @GetMapping(value = "/deleteStationMo")
    public AjaxResult deleteStationMo(String moId) {
        Integer flag = sysStationMoService.deleteStationMo(moId);
        if (flag > 0) {
            return new AjaxResult(WamingConstant.SUCCESS, "", "删除成功");
        } else {
            return new AjaxResult(WamingConstant.FAILURE, "删除失败,请确认是否绑定了监测点", "");
        }
    }

    @ApiOperation(value = "获取偏移量", notes = "获取偏移量")
    @GetMapping(value = "/getStationMoPx")
    public AjaxResult getStationMoPx(String moId) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysStationMoService.getStationMoPx(moId));
    }

    @ApiOperation(value = "获取监测点所属,类型,层级", notes = "获取监测点所属,类型,层级")
    @GetMapping(value = "/getStationMoInfo")
    public AjaxResult getStationMoInfo(String moId) {
        if (moId == null || moId.length() == 0) {
            return new AjaxResult(WamingConstant.FAILURE, "请传监测点Id", "");
        } else {
            return new AjaxResult(WamingConstant.SUCCESS, "", sysStationMoService.getStationMoInfo(moId));
        }
    }

}
